package com.skyvin.nreceipts.configuration;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.mongodb.Mongo;
import com.skyvin.nreceipts.common.util.ApplicationInterceptor;
import com.skyvin.nreceipts.security.service.AppUserDetailsService;

@Configuration
/*
 * @ImportResource("classpath:mongodb.properties")
 */public class AppConfiguration {

	/*
	 * @Value("#{databaseName}") String mongoDBName;
	 * 
	 * @Value("#{host}") String mongoDBHost;
	 * 
	 * public String mongoDBName = "nReceiptsDB"; public String mongoDBHost =
	 * "localhost";
	 */
	/*
	 * @Bean public PersistenceAnnotationBeanPostProcessor
	 * persAnnBeanPostProcessor() { return new
	 * PersistenceAnnotationBeanPostProcessor(); }
	 * 
	 * @Bean public PersistenceExceptionTranslationPostProcessor
	 * exceptionTranslationPostProcessor() { return new
	 * PersistenceExceptionTranslationPostProcessor(); }
	 * 
	 * @Bean public HibernateExceptionTranslator hibernateExceptionTranslator()
	 * { return new HibernateExceptionTranslator(); }
	 */

	@Bean
	public InternalResourceViewResolver viewResolver() {
		InternalResourceViewResolver intRes = new InternalResourceViewResolver();
		intRes.setPrefix("/jsp/");
		intRes.setSuffix(".jsp");
		intRes.setOrder(1);
		return intRes;
	}
	
	@Bean
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
		commonsMultipartResolver.setMaxUploadSize(100000);
		return commonsMultipartResolver;
	}

	@Bean
	public ContentNegotiatingViewResolver cnViewResolver() {
		ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
		Map<String, String> mediaTypes = new HashMap<String, String>();
		mediaTypes.put("html", "text/html");
		mediaTypes.put("pdf", "application/pdf");
		mediaTypes.put("xml", "application/xml");
		mediaTypes.put("json", "application/json");

		cnViewResolver.setMediaTypes(mediaTypes);
		cnViewResolver.setOrder(0);

		return cnViewResolver;
	}

	@Bean
	public AppUserDetailsService appUserDetailsService() {
		AppUserDetailsService appUserDetailsService = new AppUserDetailsService();
		return appUserDetailsService;
	}

	public @Bean
	Mongo mongo() throws UnknownHostException {
		return new Mongo("localhost");
	}

	public @Bean
	MongoDbFactory mongoDbFactory() throws Exception {
		return new SimpleMongoDbFactory(new Mongo(), "nReceiptsDB");
	}

	public @Bean
	MongoTemplate mongoTemplate() throws Exception {
		return new MongoTemplate(mongoDbFactory());
	}
	
	public @Bean
	ApplicationInterceptor applicationInterceptor() {
		return new ApplicationInterceptor();
	}
}
